//
//  MenuController.m
//  zhangchu
//
//  Created by aoyolo on 15/11/7.
//  Copyright © 2015年 李祖铜. All rights reserved.
//

#import "MenuController.h"

#import "AFNetworking.h"
#import "HomeCookingCell.h"
#import "HomeModel.h"

#import "MJRefresh.h"



@interface MenuController ()<UITableViewDataSource, UITableViewDelegate, HomeCookingCellDelegate>
{
    UIWebView *webView;
    UIButton *backBtn;
    NSDictionary *_dataDic;
    int i,totalCont,counts;
    
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *homeSerials;

@end

@implementation MenuController

- (void)viewDidLoad {
    [super viewDidLoad];
    _homeSerials = [NSMutableArray array];
    
    [_tableView registerNib:[UINib nibWithNibName:@"HomeCookingCell" bundle:nil] forCellReuseIdentifier:@"HomeCookingCellId"];
    //添加上拉刷新控件
    [self.tableView addFooterWithTarget:self action:@selector(loadHomeSerials)];
    [self loadHomeSerials];//实现网络请求数据
    
    //菜单导航栏返回按钮
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem  = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"详情页返回"] style:UIBarButtonItemStylePlain target:self action:@selector(backTo)];
}

-(void)backTo
{
    [self.navigationController popViewControllerAnimated:YES];
}



//实现网络视频加载方法
- (void)buttonDidcheak:(NSString *)fishName
{
    webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
    backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setFrame:CGRectMake(0, self.view.bounds.size.height- 650, 80, 30)];
    [backBtn setTitle:@"完成" forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
    [webView addSubview:backBtn];
    
    webView.scalesPageToFit = YES;//自动对页面进行缩放以适应屏幕
    MenuController *viewCtl = [[MenuController alloc]init];
    [viewCtl.view addSubview:webView];
    for (HomeModel *mode in _homeSerials) {
        if ([mode.title isEqualToString:fishName]) {
            [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:mode.video]]];
            [self presentViewController:viewCtl animated:YES completion:nil];
        }
    }
}
#pragma mark - 实现视频播放器返回方法
-(void)goBack:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - 请求上拉刷新的%d道菜的数据
-(void)loadHomeSerials
{
    AFHTTPRequestOperationManager  *homeCook = [AFHTTPRequestOperationManager manager];
    
    i++;
    if (i==100/6+1)
    {
        return;
    }
    
    [homeCook POST:zhangchuURL parameters:@{@"methodName":@"HomeSerial", @"page":@(i), @"serial_id":@(_serial_id), @"size":@"6", @"user_id":@"0", @"version":@"1.0"} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        //结束上拉刷新
        [self.tableView footerEndRefreshing];
        
        NSArray *arr = responseObject[@"data"][@"data"];
//        NSLog(@"%d==========", [responseObject[@"data"][@"total"]intValue]);
        totalCont = [responseObject[@"data"][@"total"]intValue];
        counts = [responseObject[@"data"][@"count"]intValue];
        self.navigationItem.title = [NSString stringWithFormat:@"%@ %d道", self.title, totalCont];
        
        for (NSDictionary *dic in arr) {
            HomeModel *model = [[HomeModel alloc]init];
            model.title = dic[@"title"];
            model.descrip = dic[@"description"];
            model.image = dic[@"image"];
            model.video = dic[@"video"];
            [self.homeSerials addObject:model];
        }
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    if (i==1)
    {
       return;
    }
    [self allert:totalCont - (i-1)*counts];
    
}
#pragma mark - 上拉显示剩余%d道菜label显示动画方法
- (void)allert:(int)count
{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth/3, ScreenHeight/2, 115, 50)];
    label.text= [NSString stringWithFormat:@"还剩下%d道菜", count];
    label.textColor = [UIColor whiteColor];
    label.alpha = 0;//消失
    label.backgroundColor = [UIColor blackColor];
    [[UIApplication sharedApplication].keyWindow addSubview:label];
    
    [UIView animateWithDuration:1 animations:^{
        label.alpha = 0.8;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1 animations:^{
            label.alpha = 0;
        } completion:^(BOOL finished) {
            [label removeFromSuperview];
        }];
    }];
}



#pragma mark - UITableViewDataSource
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 180;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _homeSerials.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.0;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeCookingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeCookingCellId"];
    cell.delegate = self;
    [cell setHomeCooking:self.homeSerials[indexPath.section]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

@end